<template>
  <div class="xtx-goods-page" v-if="Goods">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${Goods.categories[1].id}`">{{ Goods.categories[1].name }}</XtxBreadItem>
        <XtxBreadItem :to="`/category/${Goods.categories[0].id}`">{{ Goods.categories[0].name }}</XtxBreadItem>
        <XtxBreadItem >{{ Goods.name }}</XtxBreadItem>
      </XtxBread>

      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <GoodsImages :images="Goods.mainPictures"/>
          <GoodsSales />

        </div>
        <div class="spec">
          <!-- 商品名称区域 -->
          <GoodsName :goods="Goods"/>
          <!-- 商品规格区域 -->
          <GoodsSku :goods="Goods" @change="changeSku" />
          <XtxNumBox label="数量" v-model="num" :max="Goods.inventory"/>
          <XtxButton @click="insetCart()" type="primary" style="margin-top: 20px">加入购物车</XtxButton>
        </div>
      </div>

      <!-- 商品推荐 -->
      <GoodsRelevant :goodsId="Goods.id"/>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品评价 -->
          <div class="goods-tabs">
            <GoodsTabs :goods="Goods"/>
          </div>
          <!-- 注意事项 -->
          <div class="goods-warn">
            <GoodsWarn />
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :type="1" :goodsId="Goods.id"/>
          <GoodsHot :type="2" :goodsId="Goods.id"/>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import GoodsRelevant from './component/goods-relevant.vue'
import GoodsImages from './component/goods-image.vue'
import GoodsSales from './component/goods-sales.vue'
import GoodsName from './component/goods-name.vue'
import GoodsSku from './component/goods-sku.vue'
import GoodsTabs from './component/goods-tabs.vue'
import GoodsHot from './component/goods-hot.vue'
import GoodsWarn from './component/goods-warn.vue'
import { nextTick, ref, watch, provide } from 'vue'
import { useRoute } from 'vue-router'
import { findGoods } from '@/api/product'
import Message from '@/components/library/Message'
import { useStore } from 'vuex'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsRelevant,
    GoodsImages,
    GoodsSales,
    GoodsName,
    GoodsSku,
    GoodsTabs,
    GoodsHot,
    GoodsWarn
  },
  // 将数据传送给所有的子代组件
  setup () {
    const num = ref(1)
    const Goods = useGoods()
    // 修改商品的现价原价库存信息
    const changeSku = (sku) => {
      console.log(sku)
      if (sku.skuId) {
        Goods.value.price = sku.price // 价格
        Goods.value.oldPrice = sku.oldPrice // 原价
        Goods.value.inventory = sku.inventory // 库存
      }
      // 记录选择后的sku, 可能有数据,可能没有数值{}
      currSku.value = sku
    }
    provide('goods', Goods)

    const store = useStore()
    const currSku = ref(null)
    const insetCart = () => {
      // 当库存不足时提示
      if (num.value > Goods.inventory) {
        Message({ text: '库存不足', type: 'error' })
      }
      if (currSku.value && currSku.value.skuId) {
        // 所需要的参数 id skuId name attrsText picture price nowPrice selected stock count isEffective
        // 获取商品属性信息
        const { inventory, skuId, specsText } = currSku.value
        // 获取商品详细信息
        const { id, name, price, mainPictures } = Goods.value
        store.dispatch('cart/insetCart', {
          id,
          skuId,
          name,
          attrsText: specsText,
          picture: mainPictures[0],
          price,
          nowPrice: price,
          selected: true,
          stock: inventory,
          count: num.value,
          isEffective: true
        }).then(() => {
          Message({ text: '加入成功', type: 'success' })
        }).catch(e => {
          Message({ text: '加入失败', type: 'error' })
        })
      } else {
        Message({ text: '请选择完整规格' })
      }
    }

    return { Goods, changeSku, num, insetCart }
  }
}

const useGoods = () => {
  const goods = ref(null)
  const route = useRoute()

  // 侦听路由id是否有发生改变 如果有的话执行以下代码
  watch(() => route.params.id, (newVal) => {
    if (newVal && route.path === `/product/${newVal}`) {
      findGoods(route.params.id).then((data) => {
        // 由于切换商品详情的时候，组件是不会重新销毁进行加载的
        // 为了让商品商品数据为null然后使用 v-if的组件可以重新销毁和创建
        goods.value = null

        // nextTick: vue的异步延迟更新策略 => 是为了不让里面的代码立即执行,而是为了等待数据 DOM更新循环之后再执行
        nextTick(() => {
          // 获取商品详细信息数据
          goods.value = data.result
          console.log(data.result, '3')
        })
      })
    }
  }, { immediate: true })
  return goods
}
</script>

<style lang="less" scoped>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-aside {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
